//
//  WmcSelectMapAlertView.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/26.
//

import Foundation
import YPUIKit

class WmcSelectMapAlertView: WmcSelectView {
    
    var maps: [WmcMapManager.MapType] = []
    
    var clickClosure: ((WmcMapManager.MapType) -> Void)?
    
    private lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: .plain)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.register(WmcSelectMapAlertViewCell.self, forCellReuseIdentifier: "WmcSelectMapAlertViewCell")
        view.separatorStyle = .none
        return view
    }()

    override func makeUI() {
        super.makeUI()
        titleView.isShowLine = true
        titleView.leftBtn.setImage("close".wmc_image, for: .normal)
        title = "导航"
        
        addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(-Wmc_safeAreaBottom)
        }
    }
    
    override func bindViewAction() {
        titleView.leftBtn.yp.addEvent { _ in
            YPApp.CurrentVC?.dismiss(animated: true)
        }
    }
}

extension WmcSelectMapAlertView: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in _: UITableView) -> Int {
        return 1
    }

    func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
        return maps.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcSelectMapAlertViewCell = tableView.dequeueReusableCell(withIdentifier: "WmcSelectMapAlertViewCell", for: indexPath) as! WmcSelectMapAlertViewCell
        if let model = maps.yp.object(at: indexPath.row) {
            cell.bindTitle(model.name)
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let model = maps.yp.object(at: indexPath.row) {
            clickClosure?(model)
        }
    }

    func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
        return 52
    }
}


extension WmcSelectMapAlertView: YPUIPresentTarget {
    /// 显示
    func show(model: WmcMediaTeamResourceModel) -> Void {
        
        guard !model.area.isEmpty else {
            return WmcHudManager.toast("未获取到地点信息")
        }

        let maps = WmcMapManager.canOpenMaps()

        if maps.isEmpty {
            let alert = WmcAlertView()
            alert.title = "温馨提示"
            alert.msg = "请先安装地图app"
            alert.style = .confirm(title: "确定")
            alert.show()
            return
        }        
        self.maps = maps
        
        let config = YPUIPresentController.Config()
        config.contentView = self
        config.transitionAnimated.usingSpringWithDamping = .init(appearDuration: 1, disappearDuration: 1)
        config.transitionAnimated.initialSpringVelocity = .init(appearDuration: 0, disappearDuration: 0)
        config.style = .sheet(.Create({ make in
            let count = maps.count <= 2 ? 3 : maps.count
            let height = 44.wmc_autoVer + CGFloat(52 * count) + Wmc_safeAreaBottom
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(height)
        }))
        present(config: config)
        
    }
}
